<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程类别" prop="courseType">
        <el-select
          v-model="queryParams.courseType"
          placeholder="请选择课程类别"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ocss_course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属专业" prop="professionId">
        <el-select
          v-model="queryParams.professionId"
          placeholder="请选择所属专业"
          clearable
        >
          <el-option
            v-for="item in professionOptions"
            :key="item.id"
            :label="item.professionName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="校区/系" prop="deptId">
        <treeselect
          v-model="queryParams.deptId"
          :options="deptOptions"
          :show-count="true"
          style="width: 200px"
          placeholder="请选择归属校区或系"
        />
      </el-form-item>
      <el-form-item label="学期" prop="semesterId">
        <el-select
          v-model="queryParams.semesterId"
          placeholder="请选择所属学期"
          clearable
        >
          <el-option
            v-for="item in semesterOptions"
            :key="item.id"
            :label="item.semesterName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择课程状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ocss_course_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:course:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="courseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="学分" width="50" align="center" prop="credit" />
      <el-table-column label="课程类别" align="center" prop="courseType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ocss_course_type"
            :value="scope.row.courseType"
          />
        </template>
      </el-table-column>
      <el-table-column label="选课情况" align="center" prop="courseNumber">
        <template slot-scope="scope">
          <el-tag effect="dark" type="success"
            >课程数量：{{ scope.row.courseNumber }}</el-tag
          >
          <el-tag effect="dark" style="margin-top: 5px" type="warning"
            >剩余数量：{{ scope.row.remainingCourses }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="上课时段" align="center" prop="classTimer" />
      <el-table-column label="上课地点" align="center" prop="classLocation" />
      <el-table-column label="课程状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ocss_course_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="抢课开始时间"
        align="center"
        prop="courseStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseStartTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="抢课结束时间"
        align="center"
        prop="courseEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handlePublish(scope.row)"
            v-hasPermi="['system:course:edit']"
            v-if="scope.row.status == 0"
            >发布</el-button
          >
          <el-tooltip
            class="item"
            effect="dark"
            content="查看选课详情"
            placement="top"
          >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="toDetail(scope.row)"
              v-hasPermi="['system:course:detail']"
              v-if="scope.row.status != 0"
              >选课详情</el-button
            >
          </el-tooltip>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:remove']"
            v-if="scope.row.status == 0"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程名称" prop="courseName">
              <el-input
                v-model="form.courseName"
                placeholder="请输入课程名称"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="课程学分" prop="credit">
              <el-input-number
                v-model="form.credit"
                :min="1"
                :max="10"
                label="请输入学分"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任课教师" prop="teacherId">
              <el-select
                v-model="form.teacherId"
                :disabled="isTeacher"
                placeholder="请选择任课教师"
              >
                <el-option
                  v-for="item in teacherOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="课程类别" prop="courseType">
              <el-select v-model="form.courseType" placeholder="请选择课程类别">
                <el-option
                  v-for="dict in dict.type.ocss_course_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程状态" prop="status">
              <el-select
                :disabled="form.status == '0'"
                v-model="form.status"
                placeholder="请选择课程状态"
              >
                <el-option
                  v-for="dict in dict.type.ocss_course_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属专业" prop="professionId">
              <el-select
                v-model="form.professionId"
                placeholder="请选择所属专业"
              >
                <el-option
                  v-for="item in professionOptions"
                  :key="item.id"
                  :label="item.professionName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属学期" prop="semesterId">
              <el-select v-model="form.semesterId" placeholder="请选择所属学期">
                <el-option
                  v-for="item in semesterOptions"
                  :key="item.id"
                  :label="item.semesterName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="课程人数" prop="courseNumber">
              <el-input-number
                :disabled="form.status !== '0'"
                v-model="form.courseNumber"
                :min="1"
                :max="200"
                label="请输入课程人数"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校区/系" prop="deptId">
              <treeselect
                v-model="form.deptId"
                :options="deptOptions"
                :show-count="true"
                placeholder="请选择归属校区或系"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="上课时段" prop="classTimer">
              <el-input
                v-model="form.classTimer"
                placeholder="请输入上课时段"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上课地点" prop="classLocation">
              <el-input
                v-model="form.classLocation"
                placeholder="请输入上课地点"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="courseStartTime">
              <el-date-picker
                :disabled="form.status !== '0'"
                clearable
                v-model="form.courseStartTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择抢课开始时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="courseEndTime">
              <el-date-picker
                :disabled="form.status !== '0'"
                clearable
                v-model="form.courseEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择抢课结束时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="课程介绍" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
} from "@/api/system/course";

import { listTeacherAll } from "@/api/system/teacher";
import { listProfession } from "@/api/system/profession";
import { listSemester } from "@/api/system/semester";
import { deptTreeSelect } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Course",
  dicts: ["ocss_course_type", "ocss_course_status"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      deptOptions: undefined, // 校区列表
      teacherOptions: undefined, // 教师列表
      professionOptions: undefined, // 专业列表
      semesterOptions: undefined, // 学期列表
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseType: null,
        professionId: null,
        deptId: null,
        semesterId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "课程介绍不能为空", trigger: "blur" },
        ],
        credit: [{ required: true, message: "学分不能为空", trigger: "blur" }],
        courseType: [
          { required: true, message: "课程类别不能为空", trigger: "change" },
        ],
        professionId: [
          { required: true, message: "所属专业不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "校区/系不能为空", trigger: "blur" },
        ],
        semesterId: [
          { required: true, message: "学期不能为空", trigger: "blur" },
        ],
        teacherId: [
          { required: true, message: "任课教师不能为空", trigger: "blur" },
        ],
        courseNumber: [
          { required: true, message: "课程人数不能为空", trigger: "blur" },
        ],
        classTimer: [
          { required: true, message: "上课时段不能为空", trigger: "blur" },
        ],
        classLocation: [
          { required: true, message: "上课地点不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "课程状态不能为空", trigger: "change" },
        ],
        courseStartTime: [
          { required: true, message: "抢课开始时间不能为空", trigger: "blur" },
        ],
        courseEndTime: [
          { required: true, message: "抢课结束时间不能为空", trigger: "blur" },
        ],
      },
      isTeacher: false,
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getTeacherList();
    this.getSemesterList();
    this.getProfessionList();
    this.$store.getters.roles.map((r) => {
      if (r == "teacher") this.isTeacher = true;
    });
  },
  methods: {
    /** 查询校区或系下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    getTeacherList() {
      listTeacherAll().then((res) => {
        this.teacherOptions = res.data;
      });
    },
    getProfessionList() {
      listProfession().then((res) => {
        this.professionOptions = res.data;
      });
    },
    getSemesterList() {
      listSemester().then((res) => {
        this.semesterOptions = res.data;
      });
    },
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseName: null,
        description: null,
        credit: null,
        courseType: null,
        professionId: null,
        deptId: null,
        semesterId: null,
        teacherId: null,
        courseNumber: null,
        classTimer: null,
        classLocation: null,
        status: "0",
        courseStartTime: null,
        courseEndTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCourse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程信息";
      });
    },
    // 发布课程
    handlePublish(row) {
      getCourse(row.id).then((response) => {
        this.form = response.data;
        updateCourse({ ...this.form, status: "1" }).then((response) => {
          this.$modal.msgSuccess("发布成功");
          this.open = false;
          this.getList();
        });
      });
    },
    toDetail(row) {
      this.$router.push({
        path: `/course/detail/${row.id}/${row.courseName}`,
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除课程信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCourse(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
